package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.riskamount;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.medium.PolicyRiskInfoModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * description:
 * 计算风险保额
 *
 * @author jackdaw
 * @date 2020-06-07 23:39
 */
public class CalculateRiskAmount {
    /**
     * 获取风险保额
     *
     * @param riskSort  风险保额类型
     * @param models    保单集合
     * @param isCurrent true-当前单
     * @return 风险保额
     */
    public static BigDecimal getSumInsuranceRiskAmount(Set riskSort, List<PolicyRiskInfoModel> models, boolean isCurrent) {
        BigDecimal sum = BigDecimal.valueOf(0);
        if (CollectionUtils.isNotEmpty(models) && CollectionUtils.isNotEmpty(riskSort)) {
            for (PolicyRiskInfoModel policyRiskInfoModel : models) {
                //判断险种是否累积风险保额
                if (riskSort.contains(policyRiskInfoModel.getRiskCode())) {
                    InsuranceRiskAmountStrategy strategy = InsuranceRiskStrategyFactory.getStrategy(policyRiskInfoModel.getRiskCode());
                    sum = sum.add(strategy == null ?
                            policyRiskInfoModel.getAmnt() : strategy.getRiskAmount(policyRiskInfoModel, isCurrent, null));
                }
            }
        }
        return sum;
    }

    /**
     * 生成 PolicyRiskInfoModel 对象
     *
     * @param productModel 保单产品
     * @param requestModel 请求对象
     * @return
     */
    public static PolicyRiskInfoModel getPolicyRiskInfoModel(ProductModel productModel, RequestModel requestModel) {
        PolicyInfoModel policyInfoModel = requestModel.getPolicyInfo();
        AppntModel appntModel = requestModel.getCustInfo().getAppnt();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), requestModel.getCustInfo().getInsurants().getInsurant());
        PolicyRiskInfoModel policyRiskInfoModel = new PolicyRiskInfoModel();
        policyRiskInfoModel.setPrtNo(policyInfoModel.getPrtNo());
        policyRiskInfoModel.setContNo(policyInfoModel.getContNo());
        policyRiskInfoModel.setPolApplyDate(policyInfoModel.getPolApplyDate());
        policyRiskInfoModel.setRiskCode(productModel.getProdCode());
        policyRiskInfoModel.setAmnt(productModel.getAmnt());
        policyRiskInfoModel.setPrem(productModel.getPrem());
        int age = DateUtil.getYears(insurantModel.getBirthday(),
                policyInfoModel.getCvalidate());
        policyRiskInfoModel.setInsuredAppAge(age);
        policyRiskInfoModel.setInsuredBirthday(insurantModel.getBirthday());
        policyRiskInfoModel.setPayIntv(Integer.valueOf(requestModel.getProductInfo().getPayIntv()));
        if (StringUtils.equals(StrategyConstant.YEAR, productModel.getPayYearFlag())) {
            policyRiskInfoModel.setPayYears(productModel.getPayYear());
        } else if (StringUtils.equals(StrategyConstant.AGE, productModel.getPayYearFlag())) {
            policyRiskInfoModel.setPayYears(productModel.getPayYear() - age);
        }
        policyRiskInfoModel.setInsureYear(productModel.getInsureYear());
        policyRiskInfoModel.setAppntNo(appntModel.getAppntNo());
        policyRiskInfoModel.setInsuredNo(insurantModel.getInsuredNo());

        policyRiskInfoModel.setAppntBirthday(appntModel.getAppntBirthday());
        return policyRiskInfoModel;
    }
}
